package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysBank;
import com.ruoyi.project.system.service.ISysBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class SendUtils {
    @Autowired
    private RedisCache redisCache;


    @Autowired
    private ISysBankService sysBankService;
    public AjaxResult send(Map map) {
        if (null == map.get("phone")) {
            return AjaxResult.error("手机号为空");
        }
        if (null == map.get("type")) {
            return AjaxResult.error("发送短信类型不能为空");
        }
        //生成随机验证码
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        // 唯一标识
        Object uuid = IdUtils.simpleUUID();
        String verifyKey = Constants.PHONE_CODE_KEY +map.get("phone")+ uuid;
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", Constants.ACCESSKEYID, Constants.SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", map.get("phone") + "");
        request.putQueryParameter("SignName", Constants.SIGN);
        request.putQueryParameter("TemplateCode", "SMS_215495303");
        request.putQueryParameter("TemplateParam", "{\"code\":" + verifyCode + "}");
      //银行人员登录
        if (("1").equals(map.get("type") + "")) {
            SysBank sysBank=new SysBank();
            sysBank.setPhone( map.get("phone")+"");
            List<SysBank> list= sysBankService.selectSysBankList(sysBank);
            if(list.isEmpty()){
                return AjaxResult.error("手机号没在白名单!");
            }
        }
        /*  //审核是否通过
        if (("3").equals(map.get("type") + "")) {
            if ("1".equals(map.get("status"))) {
                request.putQueryParameter("TemplateCode", "SMS_200179275");
            } else if ("2".equals(map.get("status"))) {
                request.putQueryParameter("TemplateCode", "SMS_200189230");
            }
            request.putQueryParameter("TemplateParam", "{\"code\":" + verifyCode + "}");
        }
        //视频通过
        if (("5").equals(map.get("type") + "")) {
            request.putQueryParameter("TemplateCode", "SMS_200194152");
            request.putQueryParameter("TemplateParam", "{\"code\":" + map.get("subscribeId") + "}");
        }

        //重置密码
        if (("4").equals(map.get("type") + "")) {
            request.putQueryParameter("TemplateCode", "SMS_200179266");
            request.putQueryParameter("TemplateParam", "{\"code\":" + verifyCode + "}");
        }*/

        try {
            CommonResponse response = client.getCommonResponse(request);
            JSONObject jsonObject = JSONObject.parseObject(response.getData());
            if (jsonObject.getString("Code").equals("OK")) {
                redisCache.setCacheObject(verifyKey, verifyCode, Constants.PHONE_EXPIRATION, TimeUnit.MINUTES);
                return AjaxResult.success("发送成功!",uuid);
            } else {
                return AjaxResult.error(jsonObject.getString("Message"));
            }
        } catch (Exception e) {
            throw new BaseException("发送验证码失败");
        }
    }

    public AjaxResult validCode(String phone, String code, String key){
        return AjaxResult.success();
        /*if (StringUtils.isEmpty(phone)) {
            return AjaxResult.error("手机号为空");
        }
        if (StringUtils.isEmpty(code)) {
            return AjaxResult.error("短信验证码为空");
        }
        if (StringUtils.isEmpty(key)) {
            return AjaxResult.error("验证码值为空");
        }
        String verifyKey = Constants.PHONE_CODE_KEY +phone+ key;
        String captcha = redisCache.getCacheObject(verifyKey);
        if (captcha == null) {
            return AjaxResult.error("验证码失效");
        }
        if (!(code + "").equalsIgnoreCase(captcha)) {
            return AjaxResult.error("验证码错误");
        }
        return AjaxResult.success();*/
    }
}
